<?php

class Report {

	private $username;
	private $filename;

	private $xmlData;
	private $dom;
	private $xpath;

	public function __construct($username, $filename=null) {
		global $appRoot;

		$this->username = $username;
		$this->filename = $filename;

		if ($filename != null)
			$this->init($this->getReportDir()."/".$filename);
	}

	private function getReportDir() {
		global $appRoot;
		return $appRoot."data/".filenameSafe($this->username)."/reports";
	}

	private function init($path=null) {

		//Make sure relevant dirs exist
		@mkdir($this->getReportDir(), 0777, true);

		if (!isset($this->xmlData) && ($path!=null) )
			$this->xmlData = file_get_contents($path);

		if (!isset($this->dom)) {
			$this->dom = new DOMDocument();
			$this->dom->loadXML($this->xmlData);
			$this->xpath = new DOMXPath($this->dom);
		}
	}

	public function getDOM() {
		return $this->dom;
	}

	public function getXPath() {
		return $this->xpath;
	}

	public function getContent() {
		return $this->xmlData;
	}

	public function getTitle() {
		$entries = $this->xpath->query("/report/@title");
		if ($entries->length>0) {
			return $entries->item(0)->nodeValue;
		}

		return "";
	}

	public function getBuiltIn() {
		$entries = $this->xpath->query("/report/@built-in");
		if ($entries->length>0) {
			return $entries->item(0)->nodeValue;
		}

		return -1;
	}

}


?>